package com.ailanyin.admin.controller;

import com.ailanyin.admin.service.SysRoleService;
import com.ailanyin.admin.service.SysUserService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.ailanyin.model.SysRole;
import com.ailanyin.model.SysUser;
import com.ailanyin.model.SysUserRole;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 13:01
 */
@RestController
@RequestMapping("/system/role")
@Api(tags = "后台角色管理")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    @ApiOperation(value = "获取角色列表")
    @PreAuthorize("hasAuthority('system:role:list')")
    public CommonPage list(SysRole role,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> list = sysRoleService.selectRoleList(role);
        return CommonPage.restPage(list);
    }

    @PostMapping
    @ApiOperation(value = "新增角色")
    @PreAuthorize("hasAuthority('system:role:add')")
    public AjaxResult insertRole(@Validated @RequestBody SysRole role) {
        sysRoleService.insertRole(role);
        return AjaxResult.success();
    }

    @PutMapping
    @ApiOperation(value = "修改角色")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public AjaxResult updateRole(@Validated @RequestBody SysRole role) {
        sysRoleService.updateRole(role);
        return AjaxResult.success();
    }

    @GetMapping(value = "/{roleId}")
    @ApiOperation(value = "获取角色信息")
    @PreAuthorize("hasAuthority('system:role:query')")
    public AjaxResult getInfo(@PathVariable Long roleId) {
        return AjaxResult.success(sysRoleService.selectRoleById(roleId));
    }

    @GetMapping(value = "/refreshRole")
    @ApiOperation(value = "重置超级管理员权限")
    @PreAuthorize("hasAuthority('system:role:refresh')")
    public AjaxResult refreshRole() {
        sysRoleService.refreshRole();
        return AjaxResult.success();
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation(value = "批量删除角色")
    @PreAuthorize("hasAuthority('system:role:remove')")
    public AjaxResult removeRole(@PathVariable Long[] roleIds) {
        sysRoleService.deleteRoleByIds(roleIds);
        return AjaxResult.success();
    }

    @PutMapping("/dataScope")
    @ApiOperation(value = "修改保存数据权限")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public AjaxResult dataScope(@RequestBody SysRole role) {
        sysRoleService.authDataScope(role);
        return AjaxResult.success();
    }

    @PutMapping("/changeStatus")
    @ApiOperation(value = "状态修改")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public AjaxResult changeStatus(@RequestBody SysRole role) {
        sysRoleService.updateRoleStatus(role);
        return AjaxResult.success();
    }

    @GetMapping("/optionselect")
    @PreAuthorize("hasAuthority('system:role:query')")
    @ApiOperation(value = "获取角色选择框列表")
    public AjaxResult optionSelect() {
        return AjaxResult.success(sysRoleService.selectRoleAll());
    }

    @PutMapping("/authUser/selectAll")
    @ApiOperation(value = "批量选择用户授权")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds) {
        sysRoleService.insertAuthUsers(roleId, userIds);
        return AjaxResult.success();
    }

    @PutMapping("/authUser/cancelAll")
    @ApiOperation(value = "批量取消授权用户")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
        sysRoleService.deleteAuthUsers(roleId, userIds);
        return AjaxResult.success();
    }

    @PutMapping("/authUser/cancel")
    @ApiOperation(value = "取消授权用户")
    @PreAuthorize("hasAuthority('system:role:edit')")
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
        sysRoleService.deleteAuthUser(userRole);
        return AjaxResult.success();
    }

    @GetMapping("/authUser/allocatedList")
    @ApiOperation(value = "查询已分配用户角色列表")
    @PreAuthorize("hasAuthority('system:role:list')")
    public CommonPage allocatedList(SysUser user,
                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = sysUserService.selectAllocatedList(user);
        return CommonPage.restPage(list);
    }

    @GetMapping("/authUser/unallocatedList")
    @ApiOperation(value = "查询未分配用户角色列表")
    @PreAuthorize("hasAuthority('system:role:list')")
    public CommonPage unallocatedList(SysUser user,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = sysUserService.selectUnallocatedList(user);
        return CommonPage.restPage(list);
    }
}
